//
//  DeviceSettingViewController.m
//  upnpxdemo
//
//  Created by 赵帅 on 13-9-2.
//  Copyright (c) 2013年 wiimu.inc. All rights reserved.
//

#import "DeviceSettingViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface DeviceSettingViewController ()
<serviceSetSSIDDelegate,serviceMusicSyncDownDelegate,serviceSetPassDelegate>
{
    BOOL bDeleteMusic;
    BOOL bSynDownload;
}

@property (assign) IBOutlet UITextField * deviceNameField;
@property (assign) IBOutlet UILabel * deviceNameNameLabel;

@property (assign) IBOutlet UITextField * devicePassField;
@property (assign) IBOutlet UILabel * devicePassNameLabel;

@property (assign) IBOutlet UILabel * musicdownLabel;
@property (assign) IBOutlet UILabel * deletemusicLabel;
@property (assign) IBOutlet UIButton * deletemusicButton;
@property (assign) IBOutlet UILabel * syndownloadLabel;
@property (assign) IBOutlet UIButton * syndownloadButton;

@end

@implementation DeviceSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _deviceNameField.text = [[GlobalInfo sharedInstance].currentSelectedUpnPInfo objectForKey:@"ssid"];
    
    _deviceNameNameLabel.text = NSLocalizedString(@"DEVICENAME",@"");
    _devicePassNameLabel.text = NSLocalizedString(@"DEVICPASS", @"");
    _musicdownLabel.text = NSLocalizedString(@"MUSICDOWN_DOWNLOAD_LABEL", @"");
    _deletemusicLabel.text = NSLocalizedString(@"MUSICDOWN_ALSODELETEFILES", @"");
    _syndownloadLabel.text = NSLocalizedString(@"MUSICDOWN_SYNCDOWNLOAD", @"");
    
    bDeleteMusic = [[GlobalInfo sharedInstance] isMusicDeleteFile];
    bSynDownload = [[NSUserDefaults standardUserDefaults] boolForKey:@"SynDownload"];
    
    [_deletemusicButton setImage:[UIImage imageNamed:bDeleteMusic?@"chosen2":@"unchosen2"] forState:UIControlStateNormal];
    [_syndownloadButton setImage:[UIImage imageNamed:bSynDownload?@"chosen2":@"unchosen2"] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return [[GlobalInfo sharedInstance].currentSelectedUpnPInfo objectForKey:@"ssid"];
}

-(BOOL)isNewVersionButtonEnabled
{
    return YES;
}

#pragma mark - IBAction methods

-(IBAction)touchDownBackground:(id)sender
{
    [_deviceNameField resignFirstResponder];
    [_devicePassField resignFirstResponder];
}

-(IBAction)deletemusicPressed:(id)sender
{
    if(bDeleteMusic == YES)
    {
        bDeleteMusic = NO;
    }
    else
    {
        bDeleteMusic = YES;
    }
    
    [_deletemusicButton setImage:[UIImage imageNamed:bDeleteMusic?@"chosen2":@"unchosen2"] forState:UIControlStateNormal];
    
    [[GlobalInfo sharedInstance] setMusicDeleteFile:bDeleteMusic];
}

-(IBAction)syndownloadPressed:(id)sender
{
    if(bSynDownload == YES)
    {
        bSynDownload = NO;
    }
    else
    {
        bSynDownload = YES;
    }
    
    [_syndownloadButton setImage:[UIImage imageNamed:bSynDownload?@"chosen2":@"unchosen2"] forState:UIControlStateNormal];
    
    [[NSUserDefaults standardUserDefaults] setBool:bSynDownload forKey:@"SynDownload"];
    
    [[GlobalService sharedInstance] setMusicSyncDownload:self isEnable:bSynDownload];
}

#pragma mark - private methods
-(void)changeSSID
{
    [_deviceNameField resignFirstResponder];
    [[GlobalService sharedInstance] setNewSSID:self andNewSSID:_deviceNameField.text?_deviceNameField.text:@""];
    
    [self.view makeToast:NSLocalizedString(@"ALERT_CHANGESSID",@"")
                duration:3.0
                position:@"bottom"
                   title:NSLocalizedString(@"ALERT_TITLE",@"")];
}

-(void)changePass
{
    if([_devicePassField.text length] > 0 && [_devicePassField.text length] < 8)
    {
        UIAlertView * av = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ALERT_TITLE",@"") message:NSLocalizedString(@"DEVICE_SETTING_APMORETHAN8",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"WLANVIEW_CONFIRMBUTTON",@"") otherButtonTitles:nil];
        [av show];
        
        return;
    }
    
    [_devicePassField resignFirstResponder];
    [[GlobalService sharedInstance] setNewPass:self andNewPass:_devicePassField.text?_devicePassField.text:@""];
    
    [self.view makeToast:NSLocalizedString(@"ALERT_CHANGEPASS",@"")
                duration:3.0
                position:@"bottom"
                   title:NSLocalizedString(@"ALERT_TITLE",@"")];
}

#pragma mark -
#pragma mark UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField == _deviceNameField)
    {
        [self changeSSID];
    }
    else if(textField == _devicePassField)
    {
        [self changePass];
    }
    return YES;
}
#pragma mark -
#pragma mark service callbacks
-(void)serviceSetSSIDCallBack:(NSDictionary*)dicCallBack
{}
-(void)serviceMusicSyncDownCallBack:(NSDictionary*)dicCallBack
{}
-(void)serviceSetPassCallBack:(NSDictionary*)dicCallBack
{}

@end
